package com.silence.gankio.mvvm

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Context
import android.os.Bundle
import android.support.annotation.DrawableRes
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.ViewGroup
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.launcher.ARouter
import com.silence.gankio.App
import com.silence.gankio.R
import com.silence.gankio.mvvm.viewModel.BaseViewModel
import com.silence.gankio.util.StatusBarUtils
import com.xuexiang.rxutil2.rxjava.RxJavaUtils
import com.xuexiang.xui.utils.ResUtils
import com.xuexiang.xui.utils.SnackbarUtils
import com.xuexiang.xui.utils.WidgetUtils
import kotlinx.android.synthetic.main.activity_base_binding_activity.*

abstract class BaseActivity<T : BaseViewModel> : AppCompatActivity() {

    protected lateinit var mToolbar: Toolbar

    protected val mBaseModel: T? by lazy { getModelClass()?.let { ViewModelProviders.of(this).get(it) } }

    abstract fun getModelClass(): Class<T>?

    private val mLoadingDialog by lazy {
        WidgetUtils.getLoadingDialog(this, "正在加载中。。。").setIconScale(0.4F).setLoadingSpeed(8)
    }

    abstract fun getActivityContent(): Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_base_binding_activity)
        App().addActivity(this)
        StatusBarUtils.setStatusBarColor(this, resources.getColor(R.color.colorPrimary), false)
        ARouter.getInstance().inject(this)

        val inflater: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val contentView: View = inflater.inflate(getActivityContent(), null)
        content.addView(
            contentView,
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        )

        mToolbar = toolbar
        setSupportActionBar(mToolbar)
        supportActionBar?.setDisplayShowTitleEnabled(false)

        mBaseModel?.let {
            initObserveViewModel(it)
        }
    }

    private fun initObserveViewModel(baseModel: BaseViewModel) {
        baseModel.dataLoading.observe(this, Observer<Boolean> { t ->
            if (t!!) {
                mLoadingDialog.show()
            } else {
                RxJavaUtils.delay(1) { mLoadingDialog.dismiss() }
            }
        })

        baseModel.errorMsg.observe(this, Observer {
            SnackbarUtils.Indefinite(root_view, it)
                .actionColor(ResUtils.getColor(R.color.white))
                .setAction("确定") {}.danger().show()
        })
    }

    fun setToolBar(layout: Int) {
        hideToolBar()
        mToolbar = content.findViewById(layout)
        setSupportActionBar(mToolbar)
        supportActionBar?.setDisplayShowTitleEnabled(false)
    }

    fun hideToolBar() {
        mToolbar.visibility = GONE
    }

    fun setToolBarMenuOnClick(listener: Toolbar.OnMenuItemClickListener) {
        mToolbar.setOnMenuItemClickListener(listener)
    }

    fun setBackArrow() {
        val upArrow = resources.getDrawable(R.drawable.ic_arrow_back_white_36dp)
        //给ToolBar设置左侧图标
        supportActionBar?.setHomeAsUpIndicator(upArrow)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        mToolbar.setNavigationOnClickListener {
            onBackPressed()
        }
    }

    fun setRightImage(@DrawableRes img: Int) {
        toolbar_iv_right.setImageResource(img)
    }

    fun setToolBarTitle(title: CharSequence?) {
        if (!title.isNullOrEmpty()) {
            toolbar_tv_left.text = title
        }
    }

    fun getRouter(activity: String): Postcard {
        return ARouter.getInstance()
            .build(activity)
            .withTransition(R.anim.slide_in_left, R.anim.slide_in_right)
    }

    fun router(activity: String) {
        ARouter.getInstance()
            .build(activity)
            .withTransition(R.anim.nav_default_enter_anim, R.anim.nav_default_exit_anim)
            .navigation()
    }


}